package com.ilock.bean;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for Own
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.ilock.bean.Own
 * @author MyEclipse Persistence Tools
 */
public class OwnDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(OwnDAO.class);
	// property constants
	public static final String ILOCK_INFO_NUMBER = "ilockInfoNumber";
	public static final String IUSER_NUMBER = "iuserNumber";
	public static final String LEVEL = "level";

	protected void initDao() {
		// do nothing
	}

	public void save(Own transientInstance) {
		log.debug("saving Own instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Own persistentInstance) {
		log.debug("deleting Own instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	/**
	 * delete user's whole lock
	 * @param userID
	 */
	public void deleteByUserID(int userID){
		String queryString = "from Own as model where model.iuserNumber="+userID;
		List tempList = getHibernateTemplate().find(queryString);
		if(!tempList.isEmpty()){
		getHibernateTemplate().deleteAll(tempList);
		}	
	}

	public Own checkBind(int userID,int lockID){
		try {
			String queryString = "from Own as model where model.ilockInfoNumber"
					+ "="+lockID+" and model.iuserNumber="+userID;
			Own own = null;
			List list = getHibernateTemplate().find(queryString);
			if(!list.isEmpty()){
				own=(Own) list.get(0);
			}
			return own;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	public Own findById(java.lang.Integer id) {
		log.debug("getting Own instance with id: " + id);
		try {
			Own instance = (Own) getHibernateTemplate().get(
					"com.ilock.bean.Own", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Own instance) {
		log.debug("finding Own instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Own instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Own as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByIlockInfoNumber(Object ilockInfoNumber) {
		return findByProperty(ILOCK_INFO_NUMBER, ilockInfoNumber);
	}

	public List findByIuserNumber(Object iuserNumber) {
		return findByProperty(IUSER_NUMBER, iuserNumber);
	}

	public List findByLevel(Object level) {
		return findByProperty(LEVEL, level);
	}

	public List findAll() {
		log.debug("finding all Own instances");
		try {
			String queryString = "from Own";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Own merge(Own detachedInstance) {
		log.debug("merging Own instance");
		try {
			Own result = (Own) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Own instance) {
		log.debug("attaching dirty Own instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Own instance) {
		log.debug("attaching clean Own instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public List getOwnListByUserIDAndLockID(int userID,int lockID,int level){
		try {
			String queryString = "from Own as model where model.ilockInfoNumber"
					+ "="+lockID+" and model.iuserNumber="+userID+" and model.level="+level;
			List list = getHibernateTemplate().find(queryString);
			return list;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public static OwnDAO getFromApplicationContext(ApplicationContext ctx) {
		return (OwnDAO) ctx.getBean("OwnDAO");
	}
}